package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.PurchaseBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.PurchaseMapper;
import com.java2207.group1.pojo.Purchase;
import com.java2207.group1.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author AL 2022/7/27  8:56
 */
@Service("purchaseService")
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;

    @Override
    public int addPurchase(Purchase purchase) {
        return purchaseMapper.addPurchase(purchase);
    }

    @Override
    public int deletePurchase(Integer id) {
        return purchaseMapper.deletePurchase(id);
    }

    @Override
    public int updatePurchase(Purchase purchase) {
        return purchaseMapper.updatePurchase(purchase);
    }

    @Override
    public LayuiPageVo<Purchase> purchasePageDate(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        //  分页的数据
        PageHelper.startPage(page,limit);
        List<Purchase> purchases = purchaseMapper.selectByExample(params);

        //  mybatis 的分页数据结构
        PageInfo<Purchase> purchasePageInfo = new PageInfo<>(purchases);

        // 把 mybatis 的分页数据封装到 layui 中
        LayuiPageVo<Purchase> vo = new LayuiPageVo<>();
        vo.setCode(0);
        vo.setMsg("分页列表数据");
        vo.setCount(purchasePageInfo.getTotal());
        vo.setData(purchasePageInfo.getList());
        return vo;
    }

    @Override
    public List<PurchaseBo> purchaseExcelData(Map<String, Object> params) {
        List<Purchase> purchases = purchaseMapper.selectByExample(params);

        List<PurchaseBo> purchaseBos =purchases.stream().map((e) -> {
            PurchaseBo purchaseBo = new PurchaseBo();
            purchaseBo.setId(e.getId());
            purchaseBo.setMeat(e.getMeat());
            purchaseBo.setVegetable(e.getVegetable());
            purchaseBo.setFruit(e.getFruit());
            purchaseBo.setCereal(e.getCereal());
            purchaseBo.setTime(e.getTime());
            purchaseBo.setDay(e.getDay());
            return purchaseBo;
        }).collect(Collectors.toList());
        return purchaseBos;
    }


//    @Override
//    public List<PieVo> meatPieData() {
//        List<PieVo> list = purchaseMapper.meatPieData();
//        return list;
//    }
//
//    @Override
//    public List<BarVo> meatBarData() {
//        List<BarVo> list = purchaseMapper.meatBarData();
//        return list;
//    }
}
